package aod.android.com.bernay.mine.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import aod.android.com.bernay.R;
import aod.android.com.bernay.mine.activity.AlbumActivity;
import aod.android.com.bernay.mine.module.AlbumBean;
import aod.android.com.bernay.mine.module.AlbumInnerBean;
import aod.android.com.bernay.mine.module.DynamicBean;
import aod.android.com.bernay.util.MessageEvent;
import aod.android.com.bernay.util.Utils;
import aod.android.com.bernay.view.decoration.SpacesItemDecoration;

/**
 * @author crated by zhixianuanyang on 2019/4/14 13:11
 * @Email jamesKing0812@gmail.com
 */
public class AlbumViewHolder  extends RecyclerView.ViewHolder {

    private final Context mContext;
    private TextView albumDate;
    private RecyclerView albumListView;
    private CheckBox checkBox;
    private CommonAdapter<AlbumInnerBean> mAlbumDapter;
    private List<AlbumInnerBean> innerSelected = new ArrayList<>();

    public AlbumViewHolder(View itemView, Context mContext) {
        super(itemView);
        this.mContext = mContext;
        albumDate = itemView.findViewById(R.id.tv_albumDate);
        albumListView = itemView.findViewById(R.id.recyclerView);
        int leftRight = Utils.dip2px(mContext,15);
        int topBottom = Utils.dip2px(mContext,15);
        albumListView.addItemDecoration(new SpacesItemDecoration(leftRight, topBottom));
    }

    public void bindData(final AlbumBean bean, int position, final boolean isShow) {
        albumDate.setText(bean.getDate());
        final GridLayoutManager layoutManager = new GridLayoutManager(mContext, 2);
        albumListView.setLayoutManager(layoutManager);
        mAlbumDapter = new CommonAdapter<AlbumInnerBean>(mContext,R.layout.fragment_album_item, bean.getAlbumList()) {
            @Override
            protected void convert(ViewHolder holder, final AlbumInnerBean bean, final int position) {
                checkBox = holder.getView(R.id.ck_selectAlbum);
                checkBox.setChecked(bean.isSelect);
                if (!isShow){
                    checkBox.setVisibility(View.GONE);
                }else{
                    checkBox.setVisibility(View.VISIBLE);
                }

                Glide.with(mContext)
                        .load(bean.getImageUrl())
                        .into((ImageView) holder.getView(R.id.iv_albums));

                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked){
                            bean.setSelect(true);
                            if (bean.isSelect){
                                Utils.selectedList.add("选中"+position);
                            }

                        }else{
                            bean.setSelect(false);
                            if (!bean.isSelect){
                                Utils.selectedList.remove("选中"+position);
                            }
                        }
                        EventBus.getDefault().post(new MessageEvent(Utils.selectedList.size()+""));
                    }
                });
            }
        };
        albumListView.setAdapter(mAlbumDapter);
        mAlbumDapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Toast.makeText(mContext, bean.getAlbumList().size()+"",Toast.LENGTH_SHORT).show();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }
}
